package com.breakfast.fun.view.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.util.DensityUtil;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 公用标题
 */
public class TitleBarView extends RelativeLayout {
    @Bind(R.id.ll_title_back)
    LinearLayout llTitleBack;
    @Bind(R.id.iv_title_back)
    public ImageView ivTitleBack;
    @Bind(R.id.tv_title_center)
    TextView tvTitleCenter;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.tv_title_right)
    TextView tvTitleRight;
    @Bind(R.id.rl_widget_title)
    RelativeLayout rlWidgetTitle;
    private Context mContext;

    public TitleBarView(Context context) {
        super(context);
        mContext = context;
        initView();
        ButterKnife.bind(this);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
        ButterKnife.bind(this);
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.widget_title_bar, this);
    }

    /**
     * 所有都显示
     *
     * @param title
     */
    public void setAll(String title) {
        ivTitleBack.setVisibility(View.VISIBLE);
        tvTitleCenter.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setVisibility(View.GONE);
        tvTitleCenter.setText(title);
    }

    public void setTitleBg() {
        rlWidgetTitle.setBackgroundResource(R.color.transparent);
    }

    /**
     * 不设置标题
     */
    public void setNoTitle() {
        setTitleBg();
        ivTitleBack.setVisibility(View.VISIBLE);
        tvTitleCenter.setVisibility(View.GONE);
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setVisibility(View.GONE);
    }

    /**
     * 不设置右标题
     *
     * @param title
     */
    public void setTitleNoRight(String title) {
        ivTitleBack.setVisibility(View.VISIBLE);
        tvTitleCenter.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setVisibility(View.GONE);
        tvTitleCenter.setText(title);
    }

    /**
     * 设置返回按钮的可见性
     *
     * @param visibility
     */
    public void setBackVisibility(int visibility) {
        ivTitleBack.setVisibility(visibility);
    }

    /**
     * 不设置返回键
     *
     * @param title
     */
    public void setNoBack(String title, String rightTitle) {
        ivTitleBack.setVisibility(View.GONE);
        tvTitleCenter.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText(rightTitle);
        tvTitleCenter.setText(title);
    }

    /**
     * 只设置标题
     *
     * @param title
     */
    public void setStateInTitle(String title) {
        ivTitleBack.setVisibility(View.GONE);
        tvTitleCenter.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setVisibility(View.GONE);
        tvTitleCenter.setText(title);
    }

    /**
     * 设置评论标题栏
     *
     * @param title
     */
    public void setComment(String title) {
        ivTitleBack.setVisibility(View.VISIBLE);
        tvTitleCenter.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleCenter.setText(title);
    }

    /**
     * 两个标题都设置
     *
     * @param title
     * @param rightTv
     */
    public void setDoubleTitle(String title, String rightTv) {
        ivTitleBack.setVisibility(View.VISIBLE);
        tvTitleCenter.setVisibility(View.VISIBLE);
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText(rightTv);
        tvTitleCenter.setText(title);
    }

    public void setPaddingTop() {
        rlWidgetTitle.setPadding(0, 10, 0, 0);
    }

    @SuppressLint("CommitTransaction")
    public void setBack(final Activity activity) {
        llTitleBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    public void setTitleListener(OnClickListener listener) {
        tvTitleCenter.setOnClickListener(listener);
    }

    public void setBackListener(OnClickListener listener) {
        ivTitleBack.setOnClickListener(listener);
    }

    public void setRightListener(OnClickListener listener) {
        ivTitleRight.setOnClickListener(listener);
    }

    public void setRightTextListener(OnClickListener listener) {
        tvTitleRight.setOnClickListener(listener);
    }

    public void setRightImg(int resource) {
        ivTitleRight.setImageResource(resource);
    }

    public void setRightVisibility(int visibility) {
        ivTitleRight.setVisibility(visibility);
    }

    public void refreshStartAnimation(Animation animation) {
        ivTitleRight.startAnimation(animation);
    }

    public void removeRefreshAnimation() {
        ivTitleRight.clearAnimation();
    }

}
